package com.deepclassifieds.daas.api.agg.name;

import java.io.Serializable;

import com.deepclassifieds.daas.common.Builder;
import com.deepclassifieds.daas.common.Name;
import com.deepclassifieds.daas.common.Reject;

public final class AggTableName implements Builder<Name>, Serializable, Comparable<AggTableName> {

	private static final long serialVersionUID = 20110225160946L;
	
	private static final String FORMAT = "%s_%0" + Integer.toString(Integer.MAX_VALUE).length() + "d";
	
	private final String toString;
	
	public AggTableName(Name tableName, int random) {
		Reject.ifNull(tableName);
		Reject.ifNegative(random);
		this.toString = String.format(FORMAT, tableName.toString(), random);		
	}

	@Override
	public Name build() {
		return new Name(toString);
	}

	@Override
	public int hashCode() {
		return toString.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		AggTableName other = (AggTableName) obj;
		return toString.equals(other.toString);
	}

	@Override
	public String toString() {
		return toString;
	}

	@Override
	public int compareTo(AggTableName value) {
		return toString.compareTo(value.toString);
	}
	
}
